import { Component, OnInit, ChangeDetectionStrategy } from '@angular/core';
import { EzFormComponent } from '../ez-form';
import { EzFormControlComponent } from '../ez-form-control';
import { EzFormGroupComponent } from '../ez-form-group';
import { EzFormItemComponent } from '../ez-form-item';
import { EzInput } from './ez-input';
import { EzInputConfiguration } from './ez-input.configuration';

@Component({
  selector: 'ez-input',
  templateUrl: './ez-input.component.html',
  styleUrls: ['./ez-input.component.less'],
  providers: [{ provide: EzFormControlComponent, useExisting: EzInputComponent }],
})
export class EzInputComponent extends EzFormControlComponent<EzInputConfiguration, string> implements EzInput, OnInit {
  defaultMaxlength = Infinity;

  passwordVisible = false;

  constructor(
    protected ezForm: EzFormComponent,
    protected ezFormGroup: EzFormGroupComponent,
    protected ezFormItem: EzFormItemComponent
  ) {
    super(ezForm, ezFormGroup, ezFormItem);
  }

  ngOnInit(): void {
    super.ngOnInit();
  }

  checkConfiguration(configuration: EzInputConfiguration): void {
    if (configuration.search && configuration.password) {
      throw new Error(`不能同时设置[password]和[search]属性为[true]`);
    }
  }

  onSearch(event: KeyboardEvent): void {
    if (this.ezFormControlConfiguration.onSearch) {
      this.ezFormControlConfiguration.onSearch(this, this.ezFormGroup, this.ezForm);
    }
  }

  onBlur(event: KeyboardEvent): void {
    if (this.ezFormControlConfiguration.onBlur) {
      this.ezFormControlConfiguration.onBlur(this, this.ezFormGroup, this.ezForm);
    }
  }

  onFocus(event: KeyboardEvent): void {
    if (this.ezFormControlConfiguration.onFocus) {
      this.ezFormControlConfiguration.onFocus(this, this.ezFormGroup, this.ezForm);
    }
  }
}
